package com.niodata.dp.monitor.sharelib.service;

import com.niodata.dp.monitor.sharelib.entity.base.BaseResult;
import com.niodata.dp.monitor.sharelib.entity.file.AlarmStatus;
import com.niodata.dp.monitor.sharelib.view.file.FileAlarmView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component("fileAlarmOperateService")
public class FileAlarmOperateService {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;

  public BaseResult updateFileAlarm(FileAlarmView view) {
    String execUrl = String.format("http://%s:%s/monitor/file/alarm/update", server, port);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .postForEntity(execUrl, view, BaseResult.class);
    return responseEntity.getBody();
  }

  //文件告警查询
  public BaseResult listFileAlarms(Long userId, String monitorId, AlarmStatus status, int offset,
        int count) {
    monitorId = monitorId == null ? "" : monitorId;
    String alarmStatus = (status == null ? "" : status.name());
    String execUrl = String.format(
          "http://%s:%s//monitor/file/alarm/list?userId=%s&monitorId=%s&status=%s&offset=%s&count=%s",
          server, port, userId, monitorId, alarmStatus, offset, count);
    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }
}
